import React from 'react';
import { Button, Modal } from 'antd';
import { WeaTab, WeaTop } from 'ecCom';
import BaseSetting from "./BaseSetting";
import AccessItemList from "./AccessItemList";
import ScoreSetting from "./ScoreSetting";
import { inject, observer } from 'mobx-react';

/**
 * 考核设置
 * jiansong 2019/6/18
 */
@inject("baseSettingStore", "accessItemListStore", "scoreSettingStore")
@observer
export default class AssessIndex extends React.Component {

    constructor(props) {
        super(props);
        this.state = {
            selectedKey: "1",
        }
    }

    render() {
        const { selectedKey } = this.state;
        const topTab = [
            { title: "基础设置", viewcondition: "1" },
            { title: "考核指标项", viewcondition: "2" },
            { title: "分数段设置", viewcondition: "3" }
        ];
        return (
            <div style={{ height: "100vh" }}>
                <WeaTop
                    title="集团绩效基础设置"
                    icon={<i className='icon-coms-ModelingEngine-o' />}
                    iconBgcolor='rgb(150, 53, 138)'
                    buttons={this.getButton()}
                    showDropIcon={true}
                    dropMenuDatas={this.getMenu()}
                />
                <WeaTab datas={topTab}
                    keyParam="viewcondition"
                    selectedKey={selectedKey}
                    onChange={v => this.setState({ selectedKey: v })} />
                {selectedKey === "1" && <BaseSetting menu={this.getMenu()} />}
                {selectedKey === "2" && <AccessItemList menu={this.getMenu()} />}
                {selectedKey === "3" && <ScoreSetting menu={this.getMenu()} />}
            </div>
        )
    }

    getButton() {
        const { selectedKey } = this.state;
        let buttons = [];
        if (selectedKey === "1") {
            const { baseSettingStore } = this.props;
            const { baseSettingOperate, canedit, changeShowLog } = baseSettingStore;
            buttons = canedit ?
                [
                    // (<Button type='primary' onClick={() => changeShowLog()}>日志</Button>),
                    (<Button type='primary' onClick={() => baseSettingOperate({ "operation": "save" })}>保存</Button>)
                ] : []
        } else if (selectedKey === "2") {
            const { accessItemListStore } = this.props;
            if (accessItemListStore.canedit) {
                buttons = [(<Button type='primary' onClick={() => accessItemListStore.ininAddForm()}>新建</Button>)]
            }
        } else if (selectedKey === "3") {
            const { scoreSettingStore } = this.props;
            if (scoreSettingStore.canedit) {
                buttons = [
                    (<Button type='primary' onClick={() => scoreSettingStore.ininAddForm()}>新建</Button>),
                    (<Button type='primary' disabled={scoreSettingStore.selectedRowKeys.length == 0} onClick={() => scoreSettingStore.operationDel()}>删除</Button>)
                ]
            }
        }
        return buttons;
    };

    getMenu() {
        const { selectedKey } = this.state;
        let menus = [];
        if (selectedKey === "1") {
            const { baseSettingStore } = this.props;
            const { baseSettingOperate, canedit, settingData: { resourcetype, id } } = baseSettingStore;
            if (canedit) {
                menus.push({
                    key: 'save',
                    icon: <i className='icon-coms-Save-as' />,
                    content: '保存',
                    onClick: () => baseSettingOperate({ "operation": "save" }),
                })
                if (resourcetype == '2') {
                    menus.push({
                        key: '1',
                        icon: <i className='icon-coms-copy' />,
                        content: '应用到下级分部',
                        onClick: () => baseSettingOperate({ "operation": "sync" }),
                    })
                }
                if (resourcetype == '3' && id != undefined && id != '') {
                    menus.push({
                        key: '2',
                        icon: <i className='icon-coms-delete' />,
                        content: '删除设置',
                        onClick: () => {
                            Modal.confirm({
                                title: '信息确认',
                                content: '确定删除该设置?',
                                onOk: () => {
                                    baseSettingOperate({ "operation": "del" });
                                },
                            });
                        },
                    })
                }
            }
        } else if (selectedKey === "2") {
            const { accessItemListStore } = this.props;
            if (accessItemListStore.canedit) {
                menus = [{
                    key: '1',
                    icon: <i className='icon-coms-tianjia' />,
                    content: '新建',
                    onClick: () => accessItemListStore.ininAddForm()
                }]
            }
        } else if (selectedKey === "3") {
            const { scoreSettingStore } = this.props;
            if (scoreSettingStore.canedit) {
                menus = [{
                    key: '1',
                    icon: <i className="icon-coms-tianjia" />,
                    content: '新建',
                    onClick: () => scoreSettingStore.ininAddForm()
                }, {
                    key: '2',
                    icon: <i className="icon-coms-Delete" />,
                    content: '删除',
                    onClick: () => scoreSettingStore.operationDel()
                }]
            }
        }
        return menus;
    };

};